package com.mycompany.app.hr.common.menu.service.strutsmenu;

import java.util.List;

import net.sf.navigator.displayer.MenuDisplayerMapping;
import net.sf.navigator.menu.MenuComponent;
import net.sf.navigator.menu.MenuLoader;
import net.sf.navigator.menu.MenuRepository;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContextException;
import org.springframework.web.context.support.WebApplicationObjectSupport;

import com.mycompany.app.hr.common.menu.model.DatabaseMenuComponent;
import com.mycompany.app.hr.common.utility.dao.hibernate.SimpleHibernateTemplate;
import com.mycompany.app.util.SpringUtils;

public class DatabaseMenuLoader extends WebApplicationObjectSupport {
    private static Log log = LogFactory.getLog(MenuLoader.class);

    private SimpleHibernateTemplate<DatabaseMenuComponent, Long> template = null;

    public void reloadMenuRepository() {

        if (log.isDebugEnabled()) {
            log.debug("Starting struts-menu reloading....");
        }
        getServletContext().removeAttribute(MenuRepository.MENU_REPOSITORY_KEY);
        getServletContext().setAttribute(MenuRepository.MENU_REPOSITORY_KEY,
                this.loadMenuRepositoryFromDatabase());

    }

    protected MenuRepository loadMenuRepositoryFromDatabase() {

        SpringUtils.bindSession();
        List<DatabaseMenuComponent> menus = template.findAll();
        SpringUtils.unbindSession();

        if (log.isDebugEnabled()) {
            log.debug("Starting struts-menu initialization");
        }

        MenuRepository repository = new MenuRepository();
        repository.setServletContext(getServletContext());

        // add MenuDisplayer
        MenuDisplayerMapping velocity = new MenuDisplayerMapping();
        velocity.setName("Velocity");
        velocity.setType("net.sf.navigator.displayer.VelocityMenuDisplayer");
        repository.addMenuDisplayerMapping(velocity);

        // MenuDisplayerMapping tabbedMenu=new MenuDisplayerMapping();
        // velocity.setName("TabbedMenu");
        // velocity.setType("net.sf.navigator.displayer.TabbedMenuDisplayer");
        // MenuDisplayerMapping cssListMenu=new MenuDisplayerMapping();
        // velocity.setName("CSSListMenu");
        // velocity.setType("net.sf.navigator.displayer.CSSListMenuDisplayer");

        // repository.addMenuDisplayerMapping(tabbedMenu);
        // repository.addMenuDisplayerMapping(cssListMenu);

        // Load menu repository from database
        for (DatabaseMenuComponent dbMenu : menus) {
            MenuComponent mc = new MenuComponent();
            mc.setName(dbMenu.getName());
            String parent = dbMenu.getParentMenuName();
            if (parent != null) {
                MenuComponent parentMenu = repository.getMenu(parent);
                if (parentMenu == null) {
                    System.out.println("parentMenu '" + parent
                            + "' doesn't exist!");
                    // create a temporary parentMenu
                    parentMenu = new MenuComponent();
                    parentMenu.setName(parent);
                    repository.addMenu(parentMenu);
                }

                mc.setParent(parentMenu);
            }
            mc.setPage(dbMenu.getPage());
            mc.setTitle(dbMenu.getTitle());
            mc.setLocation(dbMenu.getLocation());
            repository.addMenu(mc);
        }

        if (log.isDebugEnabled()) {
            log.debug("struts-menu initialization successful");
        }

        return repository;
    }

    /**
     * Initialization of the Menu Repository.
     * 
     * @throws org.springframework.context.ApplicationContextException if an
     *             error occurs
     */
    protected void initApplicationContext() throws ApplicationContextException {

        if (template == null) {
            template = new SimpleHibernateTemplate<DatabaseMenuComponent, Long>(
                    SpringUtils.getSessionFactory(),
                    DatabaseMenuComponent.class);
        }

        getServletContext().setAttribute(MenuRepository.MENU_REPOSITORY_KEY,
                this.loadMenuRepositoryFromDatabase());

    }

}
